package top.carrotguo.weibo.ui.person;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sina.weibo.sdk.auth.AccessTokenKeeper;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.adapter.StatusAdapter;
import top.carrotguo.weibo.adapter.StringAdapter;
import top.carrotguo.weibo.bean.HomeTimeLine;
import top.carrotguo.weibo.contract.PersonContract;
import top.carrotguo.weibo.contract.PersonHomeContract;
import top.carrotguo.weibo.presenter.PersonHomePresenter;
import top.carrotguo.weibo.retrofit.callback.ErrorInfo;
import top.carrotguo.weibo.ui.base.BaseFragment;
import top.carrotguo.weibo.ui.base.MVPBaseFragment;

public class PersonHomeFragment extends MVPBaseFragment<PersonHomeContract.View,PersonHomePresenter>
    implements PersonHomeContract.View {

    @BindView(R.id.rv_info)
    RecyclerView rvInfo;
    @BindView(R.id.rl_error)
    RelativeLayout rlError;
    @BindView(R.id.tv_error)
    TextView tvError;
    //private StringAdapter mAdatper;
    //private ArrayList<String> datas;
    private RecyclerView.LayoutManager manager;
    private StatusAdapter mAdapter;
    private ArrayList<HomeTimeLine.UsersBean> mDatas = new ArrayList<>();
    public static final String UID = "UID";
    private String uid;

    public static PersonHomeFragment getInstance(String uid){
        PersonHomeFragment fragment = new PersonHomeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(UID,uid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void getBundleExtras(Bundle bundle) {
        uid = bundle.getString(UID);
    }

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view =  LayoutInflater.from(mContext).inflate(R.layout.fragment_person_home,container,false);
        return view;
    }

    @Override
    protected void initLogic() {
        ButterKnife.bind(this, mRootView);
        manager = new LinearLayoutManager(mContext);
        rvInfo.setLayoutManager(manager);
        mAdapter = new StatusAdapter(mContext,mDatas);
        rvInfo.setAdapter(mAdapter);
        mPresenter.getBlogList(AccessTokenKeeper.readAccessToken(mContext),uid);
    }

    @Override
    protected PersonHomePresenter createPresenter() {
        return new PersonHomePresenter();
    }

    @Override
    public void getBlogsSuccess(HomeTimeLine successInfo) {
        rlError.setVisibility(View.GONE);
        rvInfo.setVisibility(View.VISIBLE);
        mDatas.addAll(successInfo.getStatuses());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getBlogsError(ErrorInfo errorInfo) {
        rvInfo.setVisibility(View.GONE);
        rlError.setVisibility(View.VISIBLE);
        tvError.setText(errorInfo.getErrorMsg());
    }

    @Override
    public void showBlogsEmpty() {
        rvInfo.setVisibility(View.GONE);
        rlError.setVisibility(View.VISIBLE);
        tvError.setText("你没有发微博呀！");
    }
}
